package com.sudoku.ui.content;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.sudoku.common.Constants;
import com.sudoku.listener.ButtonListener;
import com.sudoku.ui.Sudoku;

public class SHome extends JPanel {
	private static final long serialVersionUID = -8821585740865397045L;

	Sudoku sudoku;
	JButton easy;
	JButton intermediate;
	JButton difficult;
	JLabel welcomeLabel;
	JLabel subtitleLabel;
	JLabel infoLabel;
	JLabel copyrightLabel;
	JLabel supportLabel;
	
	public SHome(Sudoku sudoku) {
		this.sudoku = sudoku;
		this.setLayout(null); // 采用空布局，手动指定各组件位置
		
		/**
		 * 初始化各组件
		 */
		// 欢迎语
		welcomeLabel = new JLabel("Welcome!");
		welcomeLabel.setHorizontalAlignment(JLabel.CENTER);
		welcomeLabel.setFont(Constants.FONT_WELCOME);
		this.add(welcomeLabel);
	
		
		// 副标题
		subtitleLabel = new JLabel("A Sudoku Game Made by Corona.");
		subtitleLabel.setHorizontalAlignment(JLabel.CENTER);
		subtitleLabel.setFont(Constants.FONT_SUBTITLE);
		this.add(subtitleLabel);
		
		// 提示信息
		infoLabel = new JLabel("Please Choose Level of Game.");
		infoLabel.setHorizontalAlignment(JLabel.CENTER);
		infoLabel.setFont(Constants.FONT_INFO);
		this.add(infoLabel);
		
		// 作者信息
		copyrightLabel = new JLabel("Copyright(C)Corona 2021");
		copyrightLabel.setHorizontalAlignment(JLabel.CENTER);
		copyrightLabel.setFont(Constants.FONT_COPYRIGHT);
		this.add(copyrightLabel);
		
		// 充电二维码
		supportLabel = new JLabel(new ImageIcon(SHome.class.getResource("/config/qrcode.png")));
		supportLabel.setVisible(true);
		this.add(supportLabel);
		
		// “简单”难度的按钮
		easy = new JButton(Constants.LABEL_EASY);
		easy.setFont(Constants.FONT_BUTTON);
		easy.setFocusPainted(false); // 不显示焦点框
		easy.addMouseListener(new ButtonListener(sudoku, getEasyInfo(), infoLabel, Constants.VALUE_EASY));
		this.add(easy);
		
		// “普通”难度的按钮
		intermediate = new JButton(Constants.LABEL_INTERMEDIATE);
		intermediate.setFont(Constants.FONT_BUTTON);
		intermediate.setFocusPainted(false);
		intermediate.addMouseListener(new ButtonListener(sudoku, getIntermediateInfo(), infoLabel, Constants.VALUE_INTERMEDIATE));
		this.add(intermediate);
		
		// “困难”难度的按钮
		difficult = new JButton(Constants.LABEL_DIFFICULT);
		difficult.setFont(Constants.FONT_BUTTON);
		difficult.setFocusPainted(false);
		difficult.addMouseListener(new ButtonListener(sudoku, getDifficultInfo(), infoLabel, Constants.VALUE_DIFFFICULT));
		this.add(difficult);
	
		/**
		 * 一些布局过程中用来确定组件位置的常量
		 */
		final int WIDTH = 540;
		// 按钮的宽度与长度
		final int bWidth = (int)(WIDTH*0.20);
		// 按钮之间的间隔
		final int bGap = (int)(WIDTH*0.05);
		// intermediate 按钮左上角的x坐标
		final int x = (int)(0.5*(WIDTH-bWidth*2));
		/**
		 * 通过setBounds方法进行手动布局；
		 */
		welcomeLabel.setBounds(W(0.1), H(0.05), W(0.8), H(0.2));
		subtitleLabel.setBounds(W(0.1), H(0.20), W(0.8), H(0.1));
		easy.setBounds(x-bGap-bWidth, H(0.35), bWidth, H(0.08));
		intermediate.setBounds(x, H(0.35), bWidth*2, H(0.08));
		difficult.setBounds(x+bWidth*2+bGap, H(0.35), bWidth, H(0.08));
		infoLabel.setBounds(W(0.05), H(0.40), W(0.9), H(0.15));
		supportLabel.setBounds(W(0.35), H(0.6), W(0.3), H(0.3));
		copyrightLabel.setBounds(W(0.2), H(0.92), W(0.6), H(0.1));
		this.setBackground(Constants.CLOSED_CELL_BGCOLOR);
	}
	
	private static int W(double ratio) {
		final int WIDTH = 540;
		return (int)(WIDTH*ratio);
	}
	
	private static int H(double ratio) {
		final int HEIGHT = 540;
		return (int)(HEIGHT*ratio);
	}
	private String getEasyInfo() {
		return "Easy Mode: You Need to Fill about " + (100-(int)(Constants.RATIO_EASY*100)) + "% of Cells";
	}
	
	private String getIntermediateInfo() {
		return "Intermediate Mode: You Need to Fill about " + (100-(int)(Constants.RATIO_INTERMEDIATE*100)) + "% of Cells";
	}
	
	private String getDifficultInfo() {
		return "Difficult Mode: You Need to Fill about " + (100-(int)(Constants.RATIO_DIFFICULT*100)) + "% of Cells";
	}
}
